{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "# _*Exercises*_\n",
    "\n",
    "The latest version of this notebook is available on https://github.com/qiskit/qiskit-tutorial.\n",
    "\n",
    "This notebook is aimed to help you understand the content of notebooks in this folder by working on the problems listed.\n",
    "\n",
    "The content of this exercise is based on the notebook [here](https://github.com/Qiskit/qiskit-tutorials/blob/master/qiskit/terra/quantum_circuits.ipynb).  \n",
    "\n",
    "You'll need to run the cells to use this tutorial. To run a cell, do the following.\n",
    "\n",
    "* For laptops and desktops, click on the cell and press **Shift-Enter**.\n",
    "\n",
    "* For mobile devices, tap on the icon that appears to the left of a cell.\n",
    "\n",
    "Get started by doing this for the cell below (it will take a second or two to run)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#importing array and useful math functions\n",
    "import numpy as np\n",
    "\n",
    "#importing circuits and registers\n",
    "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister\n",
    "\n",
    "#importing backends and running environment\n",
    "from qiskit import BasicAer, execute"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Problem 1: Playing with Circuits\n",
    "\n",
    "Q: Prepare two quantum registers each of which has size 2 qubits, and a quantum register consisting of 4 bits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Create a circuit using the above registers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Apply NOT gate and the Hadamard gate to the the first qubit of the first register. Then, apply the controlled NOT with the first qubit as control on the first register."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apply the Hadamard gate to the second qubit of the second register. Then, apply the controlled NOT with the second qubit as control on the second register."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Draw the circuit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Create another circuit consisting of the same register as the first circuit. Apply the controlled NOT gate with the second qubit of the first register as control and the first qubit of the second register as target to the circuit. And, draw the circuit. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: What are the numbers of tensor factors of the first and second circuits? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Create another circuit by appending the second circuit to the first circuit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Create a measurement circuit that store the result of measuring quantum registers to the classical register."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Add the measurement circuit to the combined quantum circuit by first applying barrier. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: What is the number of tensor factor of the final circuit? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Run the circuit on the local simulator backend."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 2: Circuit Equivalence\n",
    "\n",
    "Q: Create a quantum circuit that consists of a two-qubit quantum register and a two-bit classical register. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Swap gate\n",
    "\n",
    "Q: Add three controlled NOT gates to the circuit as follows: apply the controlled NOT with the first qubit as control, and then the controlled NOT with the second qubit as control, and the controlled NOT with the first qubit as control. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Show that the above circuit swaps the first qubit to the second qubit. \n",
    "(*HINT: Straightforwardly by checking all possible classical inputs (bases) and seeing the outputs*)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Create another circuit that consists of three controlled NOT gates on the same registers as the first circuit as follows: apply the controlled NOT with the second qubit as control, and then the controlled NOT with the first qubit as control, and the controlled NOT with the second qubit as control. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Show that the above circuit is equal to the first swap circuit. (*HINT: append the second circuit to the first circuit and run the resulting circuit on all possible input bits*)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem 3: Equivalence up to the Global Phase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Create a quantum circuit which consist of a two-qubit quantum register and a two-bit classical register. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Using the above circuit, show that the X (or, NOT) gate is equal to the $u3$ gate with parameters $(\\pi, 0, \\pi)$. Those gates are equivalence up to the global phase. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q: Using the above circuit, show that the controlled NOT gate is not equal to the controlled-$u3(\\pi,0,\\pi)$ gate. Notice that eventhough two single-qubit gates are equivalent up to the global phase, their controlled gates can be different. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Contributing\n",
    "\n",
    "We welcome feedback and new problems to be added to this notebook."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "QISKitenv",
   "language": "python",
   "name": "qiskitenv"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
